package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.modules.archives.entity.UserQualification;
import com.lantosec.hmrp.modules.archives.mapper.UserQualificationMapper;
import com.lantosec.hmrp.modules.archives.service.IUserQualificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserQualificationVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 执业资格信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-07-13
 */
@Service
@Primary
@Log4j2
public class UserQualificationServiceImpl extends ServiceImpl<UserQualificationMapper, UserQualification> implements IUserQualificationService {
    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public List<UserQualificationVO> getAll(String userId) {
        List<UserQualificationVO> userQualificationVOS = new ArrayList<>();
        List<UserQualification> userQualifications = this.list(new QueryWrapper<UserQualification>().eq(UserQualification.ColName.user_id.toString(),userId));
        if(CollectionUtils.isNotEmpty(userQualifications)){
            userQualifications.forEach(userQualification->{
                UserQualificationVO userQualificationVO = userQualification.convertToVO();
                //返回附件路径
                String photos = userQualification.getPhotos();
                if(StringUtils.isNotEmpty(photos)){
                    String[] photo = photos.split(",");
                    List<String> list = new ArrayList<>();
                    for(String s:photo){
                        SysUploads sysUploads = iSysUploadsService.getById(s);
                        String savePath = sysUploads.getSavePath();
                        String fileName = sysUploads.getFileName();
                        list.add(InitDataListener.uploadsUrl + savePath + fileName);
                    }
                    userQualificationVO.setSavePath(list);
                }
                userQualificationVOS.add(userQualificationVO);
            });
        }
        return userQualificationVOS;
    }
}
